using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Web.Configuration;
using TntOrm.Structures;

namespace OrmAdmin
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        private TntOrm.OrmManager _Manager;

        public TntOrm.OrmManager Manager
        {
            get { return _Manager; }
            set { _Manager = value; }
        }

        private OrmAdmin.Structs.Settings _Settings;

        public OrmAdmin.Structs.Settings Settings
        {
            get { return _Settings; }
            set { _Settings = value; }
        }

        private string _FlashText;

        public string FlashText
        {
            get { return FlashLiteral.Text; }
            set 
            {
                _FlashText += value + "<br/>";
                FlashLiteral.Text = "<pre>" + _FlashText + "</pre>";
            }
        }

        private bool IsInitilize = false;

        public void InitializeOrm( )
        {
            if( Session[ "SettingsList" ] == null )
            {
                LoadSettings( );
                Session[ "SettingsList" ] = Settings;
            }
            else
            {
                Settings = ( OrmAdmin.Structs.Settings ) Session[ "SettingsList" ];
            }
            
            if( Session[ "OrmManager" ] == null )
            {
                string AssemblyName = Settings.GetSetting( "ObjectsAssemblyName" );

                TntOrm.AssemblyManager Am = new TntOrm.AssemblyManager( );

                Am.LoadAssembly( Server.MapPath( "bin/" + AssemblyName ) );
                Am.SetObjectToOrm( );

                Session[ "OrmManager" ] = TntOrm.OrmManager.GetInstance( );
            }            

            _Manager = ( TntOrm.OrmManager ) Session[ "OrmManager" ];

            IsInitilize = true;
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            if( !IsInitilize )
            {
                InitializeOrm( );
            }

            if( !IsPostBack )
            {
                SetMenu( );
            }
        }

        private void LoadSettings( )
        {
            Settings = new OrmAdmin.Structs.Settings( );
            Settings.SettingsList = new System.Collections.Generic.Dictionary<string, string>( );

            System.Collections.Specialized.NameValueCollection AppSettings
                    = WebConfigurationManager.GetWebApplicationSection( "appSettings" )
                    as System.Collections.Specialized.NameValueCollection;

            foreach( string Key in AppSettings.Keys )
            {
                Settings.AddSetting( Key, AppSettings[ Key ] );
            }
        }

        public void OpenConnection( )
        {
            ConnectionStringsSection ConnectionStrings
                = WebConfigurationManager.GetWebApplicationSection( "connectionStrings" ) as ConnectionStringsSection;
            
            TntOrm.Utility.SqlHelper.Connection 
                = new System.Data.SqlClient.SqlConnection( ConnectionStrings.ConnectionStrings[ "SiteDataBase" ].ConnectionString );
        }

        public void SetMenu( )
        {
            foreach( TableMapping ObjectMap in _Manager.TableDict.Values )
            {
                if( ObjectMap.InMenu )
                {
                    HyperLink ObjectLink = new HyperLink( );
                    ObjectLink.Text = ObjectMap.TableTitle;
                    ObjectLink.NavigateUrl = "ListForm.aspx?obj=" + ObjectMap.TableName;
                    MenuPlaceHolder.Controls.Add( ObjectLink );

                    Panel BrPanel = new Panel( );
                    MenuPlaceHolder.Controls.Add( BrPanel );
                }
            }
        }
    }
}